package com.cosl.ipoc.pm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnpc.eisc.base.pojo.PageInfo;
import com.cnpc.eisc.base.pojo.User;
import com.cosl.ipoc.pm.dto.IpocPmJobTrackingDetailResDto;
import com.cosl.ipoc.pm.dto.IpocPmJobTrackingMainResDto;
import com.cosl.ipoc.pm.dto.IpocPmJobTrackingTemplateResDto;
import com.cosl.ipoc.pm.entity.IpocPmJobTrackingMain;
import com.cosl.ipoc.pm.entity.IpocPmJobTrackingMainExamine;
import com.cosl.ipoc.pm.mapper.IpocPmJobTrackingDetailDao;
import com.cosl.ipoc.pm.entity.IpocPmJobTrackingDetail;
import com.cosl.ipoc.pm.mapper.IpocPmJobTrackingMainDao;
import com.cosl.ipoc.pm.mapper.IpocPmJobTrackingMainExamineDao;
import com.cosl.ipoc.pm.mapper.IpocPmJobTrackingTemplateDao;
import com.cosl.ipoc.pm.service.IpocPmJobTrackingDetailService;
import com.cosl.ipoc.pm.service.IpocPmJobTrackingMainService;
import com.cosl.ipoc.pm.util.BeanConverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* <p>
* 模板管理-作业关键点跟踪模板表 服务实现类
* </p>
*
* @author endure
* @since 2025-06-26
*/
@Service
@Primary
public class IpocPmJobTrackingDetailServiceImpl extends ServiceImpl<IpocPmJobTrackingDetailDao, IpocPmJobTrackingDetail> implements IpocPmJobTrackingDetailService {
    @Autowired
    private IpocPmJobTrackingDetailDao ipocPmJobTrackingDetailDao;
    @Autowired
    private IpocPmJobTrackingMainDao ipocPmJobTrackingMainDao;
    @Autowired
    private IpocPmJobTrackingMainExamineDao ipocPmJobTrackingMainExamineDao;
    @Autowired
    private IpocPmJobTrackingTemplateDao ipocPmJobTrackingTemplateDao;

    @Autowired
    private IpocPmJobTrackingMainService ipocPmJobTrackingMainService;

    @Override
    public PageInfo<IpocPmJobTrackingDetailResDto> pageList(int pageNum, int pageSize, Map<String, Object> params, User user) {


        List<IpocPmJobTrackingTemplateResDto> list = ipocPmJobTrackingTemplateDao.selectByList(params);


        List<IpocPmJobTrackingDetailResDto> listdetail = ipocPmJobTrackingDetailDao.selectByList(params);
        // 同步
        if (listdetail == null || listdetail.isEmpty()) {

            List<IpocPmJobTrackingDetail> ipocPmJobTrackingDetailList = BeanConverUtil.converList(list, IpocPmJobTrackingDetail.class);
            ipocPmJobTrackingDetailList.stream().forEach(item -> {
                item.setIpocPmJobTrackingDetailId(UUID.randomUUID().toString().replace("-", ""));
                item.setCreateDate(new Date());
                item.setWellId(params.get("wellId").toString());
                item.setBsflag("0");
            });
            this.saveBatch(ipocPmJobTrackingDetailList);
            listdetail = BeanConverUtil.converList(list, IpocPmJobTrackingDetailResDto.class);
        }

        //查询
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(listdetail.size());
        pageInfo.setRows(listdetail);

        //更新主表默认钻井类型
        Map<String, Object> params2 = new HashMap<>();
//        params2.put("wellId",params.get("wellId").toString());
        params2.put("platformNumber",params.get("platformNumber").toString());
        List<IpocPmJobTrackingMainResDto> olist = ipocPmJobTrackingMainDao.selectByList(params2);
        Object codeObj = params.get("drillingRigTypeCode");
        for (IpocPmJobTrackingMainResDto item : olist) {
            if(codeObj != null) {
                String codeStr = codeObj.toString();
                if(StringUtils.isNotEmpty(codeStr) && codeStr.equals(item.getDrillingRigTypeCode())) {
                    item.setDrillingRigTypeCodeStatus("1");
                }else {
                    item.setDrillingRigTypeCodeStatus("0");
                }
            }
        }
        List<IpocPmJobTrackingMain> ilist  = BeanConverUtil.converList(olist, IpocPmJobTrackingMain.class);
        ipocPmJobTrackingMainService.updateBatchById(ilist);

        return pageInfo;
    }
    @Override
    public String examine(Map<String, Object> params, String statu, User user) {
        // 查询数据
        List<IpocPmJobTrackingMain> list = ipocPmJobTrackingDetailDao.selectOne(params);
        if (CollectionUtils.isEmpty(list)) {
            return "数据不存在";
        }

        // 更新审核状态
        IpocPmJobTrackingMain main = list.get(0);
        main.setExamineStatus(statu);
        main.setImplementationStatus("1");
        if(params.get("examineUser")!=null) {
            main.setExamineUser(params.get("examineUser").toString());
        }

        ipocPmJobTrackingMainDao.updateById(main);

        IpocPmJobTrackingMainExamine examine = new IpocPmJobTrackingMainExamine();
        examine.setExamineStatus(statu);
        examine.setBsflag("0");
        examine.setCreateUser(user.getUserName());
        examine.setCreateDate(new Date());
        examine.setSessionsCode(params.get("sessionsCode").toString());
        examine.setDrillingRigTypeCode(params.get("drillingRigTypeCode").toString());
        examine.setWellId(params.get("wellId").toString());
        if(params.get("remark") != null) {
            examine.setRemark(params.get("remark").toString());
        }
        ipocPmJobTrackingMainExamineDao.insert(examine);

        return "操作成功";
    }

    @Override
    public IpocPmJobTrackingMain checkStatu(Map<String, Object> params, User user) {
        // 查询数据
        List<IpocPmJobTrackingMain> list = ipocPmJobTrackingDetailDao.selectOne(params);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }


    @Override
    public List<IpocPmJobTrackingMainExamine> selectExamineList(Map<String, Object> params, User user) {
        // 查询数据
        List<IpocPmJobTrackingMainExamine> list = ipocPmJobTrackingDetailDao.selectExamineList(params);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list;
    }

    @Override
    public  List<IpocPmJobTrackingDetailResDto> exportShowStat(Map<String,Object> params){
        List<IpocPmJobTrackingDetailResDto> list = ipocPmJobTrackingDetailDao.selectByList(params);
        //将落实详情字段的01转为中文
        for (IpocPmJobTrackingDetailResDto dto : list) {
            if(StringUtils.isNotEmpty(dto.getImplementationStatus())){
                if(dto.getImplementationStatus().equals("1")){
                    dto.setImplementationStatus("已落实");
                }else if(dto.getImplementationStatus().equals("0")){
                    dto.setImplementationStatus("未落实");
                }

            }
        }
        return list;
    }


}